package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.GoodsTypeMapper;
import com.gxa.zhonglong.pojo.GoodsType;
import com.gxa.zhonglong.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author NITRO
 */

@Service("goodsTypeService")
@Transactional(rollbackFor = {Throwable.class})
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
   private GoodsTypeMapper goodsTypeMapper;

    @Override
    public Boolean add(GoodsType goodsType) throws BusinessException {
        GoodsType goodsType1 = goodsTypeMapper.selectByCode(goodsType.getCode());
        if(goodsType1==null){
            goodsTypeMapper.add(goodsType);
            return true;
        }else{
            throw new BusinessException(ExceptionCode.ADD_DEFEATED);
        }
    }

    @Override
    public void delete(List<Long> ids) throws BusinessException {
        if (goodsTypeMapper.delete(ids)<1) {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
    }

    @Override
    public Boolean update(GoodsType goodsType) throws BusinessException {
        Boolean aBoolean = goodsTypeMapper.update(goodsType);
        if(aBoolean){
            return true;
        }else{
            throw  new BusinessException(ExceptionCode.UPDATE_FALSE);
        }
    }

    @Override
    public List<GoodsType> getList() {
        List<GoodsType> select = goodsTypeMapper.select();
        return select;
    }
    @Override
    public List<String> getNameList() {
        List<GoodsType> select = goodsTypeMapper.select();
        List<String> typeNameList=new ArrayList<>();
        for (GoodsType goodsType : select) {
            String name = goodsType.getName();
            typeNameList.add(name);
        }
        return typeNameList;
    }
}
